package com.gxuwz.attend.web.servlet.instructor;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxuwz.attend.context.AppContext;
import com.gxuwz.attend.entity.Instructor;
import com.gxuwz.attend.entity.Leave;
import com.gxuwz.attend.service.instructor.ExportService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
@WebServlet("/export")
public class ExportServlet extends HttpServlet {
    private ExportService exportService;

    @Override
    public void init() throws ServletException {
        // 从Spring容器中获取LeaveApplicationService
        exportService = AppContext.getBean(ExportService.class);
    }
    // 处理查询请求，显示请假单
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取辅导员工号 (instID) 和学期 (semester) 参数
        HttpSession session = request.getSession();
        Instructor instructor = (Instructor) session.getAttribute("currentUser");
        String instID = instructor.getInstID();  // 从 Session 中获取辅导员工号

        String term = request.getParameter("term"); // 学期（如：第一学期或第二学期）
        session.setAttribute("term",term);

        // 设置响应的内容类型为 JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        // 调用 Service 层获取请假记录
        List<Leave> leaveList = exportService.getLeave(term, instID);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(leaveList);
        response.getWriter().write(json);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取辅导员工号 (instID) 和学期 (semester) 参数
        HttpSession session = request.getSession();
        Instructor instructor = (Instructor) session.getAttribute("currentUser");
        String instID = instructor.getInstID();  // 从 Session 中获取辅导员工号

      String term = (String) session.getAttribute("term");
        System.out.println(instID);
        System.out.println(term);
        // 调用 Service 层获取请假记录
        List<Leave> leaveList = exportService.getLeave(term, instID);

        try {
            String filePath = "D:/term.xlsx";
            exportService.exportToExcel(leaveList, filePath);

            // 设置响应为下载文件
            response.setContentType("application/vnd.ms-excel");//设置 HTTP 响应的内容类型为 Excel 文件，以便浏览器识别和处理
            response.setHeader("Content-Disposition", "attachment; filename=term.xlsx");//设置响应头，指示浏览器下载该文件，并命名为 term.xlsx

            FileInputStream fileInputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            fileInputStream.close();
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
